home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ABUSESRC.ZIP / AbuseSrc / imlib / include.c < prev    next >
C/C++ Source or Header  |  1996-04-11  |  2KB  |  60 lines

  1. #include "include.hpp"
  2. #include "ctype.h"
  3.  
  4. void write_include(image *im, palette *pal, char *filename, char *name)
  5. {
  6.   char tmp_name[200];
  7.   strcpy(tmp_name,name);
  8.   int j,append=0,i;
  9.   for (j=0;j<strlen(name);j++)
  10.     if (toupper(tmp_name[j])<'A' || toupper(tmp_name[j])>'Z')
  11.       tmp_name[j]='_';
  12.  
  13.   FILE *fp=fopen(filename,"rb");  // see if the file already exsist
  14.   if (fp)
  15.   {
  16.     fclose(fp);
  17.     fp=fopen(filename,"ab");  // if so, append to the end and don't write the palette
  18.     append=1;
  19.   }
  20.   else fp=fopen(filename,"wb");
  21.  
  22.   if (!fp)
  23.     set_error(imWRITE_ERROR);
  24.   else
  25.   {
  26.     fprintf(fp,"/* File produced by Satan Paint (c) 1994 Jonathan Clark */\n\n");
  27.     if (!append)
  28.     {
  29.       fprintf(fp,"unsigned char %s_palette[256*3] = {\n    ",tmp_name);
  30.       unsigned char *p=(unsigned char *)pal->addr();
  31.       for (i=0;i<768;i++,p++)
  32.       {
  33.     fprintf(fp,"%d",(int)*p);
  34.     if (i==767) 
  35.         fprintf(fp,"};\n\n");
  36.     else
  37.         if (i%15==14)
  38.     fprintf(fp,",\n    ");
  39.         else fprintf(fp,", ");
  40.       }
  41.     }
  42.     fprintf(fp,"unsigned char %s[%d*%d]={\n    ",tmp_name,
  43.             im->width(),im->height()); 
  44.     int x,y,max=im->width()*im->height()-1;
  45.     for (y=0,i=0;y<im->height();y++)
  46.       for (x=0;x<im->width();x++,i++)
  47.       {
  48.         fprintf(fp,"%d",(int)im->pixel(x,y));
  49.         if (i==max)
  50.           fprintf(fp,"};\n\n");
  51.         else
  52.           if (i%15==14)
  53.             fprintf(fp,",\n    ");
  54.           else fprintf(fp,", ");
  55.       }
  56.   }
  57.   fclose(fp);
  58. }
  59.  
  60.